# config/config_manager.py
"""
配置管理器 - 负责保存和恢复雷达参数、目标参数和仿真参数
"""

import json
import os
from typing import Dict, List, Any


class ConfigManager:
    """配置管理器 - 处理参数的保存和加载"""

    def __init__(self, config_file: str = "radar_config.json"):
        """
        初始化配置管理器

        Args:
            config_file: 配置文件路径
        """
        self.config_file = config_file

    def save_config(self, radar_params: Dict, targets: List[Dict], simulation_params: Dict) -> bool:
        """
        保存配置到文件

        Args:
            radar_params: 雷达参数字典
            targets: 目标列表（每个目标是字典）
            simulation_params: 仿真参数字典

        Returns:
            bool: 保存是否成功
        """
        try:
            config_data = {
                'radar_params': radar_params,
                'targets': targets,
                'simulation_params': simulation_params
            }

            with open(self.config_file, 'w', encoding='utf-8') as f:
                json.dump(config_data, f, indent=4, ensure_ascii=False)

            print(f"[ConfigManager] 配置已保存到: {self.config_file}")
            return True

        except Exception as e:
            print(f"[ConfigManager] 保存配置失败: {e}")
            return False

    def load_config(self) -> Dict[str, Any]:
        """
        从文件加载配置

        Returns:
            Dict: 包含radar_params, targets, simulation_params的字典
                  如果文件不存在或加载失败，返回None
        """
        try:
            if not os.path.exists(self.config_file):
                print(f"[ConfigManager] 配置文件不存在: {self.config_file}")
                return None

            with open(self.config_file, 'r', encoding='utf-8') as f:
                config_data = json.load(f)

            print(f"[ConfigManager] 配置已加载: {self.config_file}")
            return config_data

        except Exception as e:
            print(f"[ConfigManager] 加载配置失败: {e}")
            return None

    def config_exists(self) -> bool:
        """检查配置文件是否存在"""
        return os.path.exists(self.config_file)
